package cn.gson.crm.control.student;

import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crm.control.base.BaseServlet;
import cn.gson.crm.model.dao.AllotDao;
import cn.gson.crm.model.dao.ClazzDao;
import cn.gson.crm.model.dao.InterestingDao;
import cn.gson.crm.model.dao.SourceDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.entity.Clazz;
import cn.gson.crm.model.entity.Interesting;
import cn.gson.crm.model.entity.Source;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.entity.StudentVo;
import cn.gson.crm.model.po.StaffPo;

/**
 * Servlet implementation class StaffServlet
 */
@WebServlet("/student")
public class StudentServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private StudentDao  stu=new StudentDao();
	private InterestingDao  in=new InterestingDao();
	private SourceDao  source=new SourceDao();
	private AllotDao allotDao = new AllotDao();
	private ClazzDao clazz=new ClazzDao();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String method=request.getParameter("method");
			if("login".equalsIgnoreCase(method)){
				login(request,response);
			}
			if("select".equalsIgnoreCase(method)){
				select(request,response);
			}
			if("delete".equalsIgnoreCase(method)){
				delete(request,response);
			}
			if("insert".equalsIgnoreCase(method)){
				insert(request,response);
			}
			if("update".equalsIgnoreCase(method)){
				update(request,response);
			}
			if("selectby".equalsIgnoreCase(method)){
				selectby(request,response);
			}
			if("updateStudent".equalsIgnoreCase(method)){
				updateStudent(request,response);
			}
			if("forward".equalsIgnoreCase(method)){
				forward(request,response);
			}
			if("selectgroup".equalsIgnoreCase(method)){
				selectgroup(request,response);
			}
			if("selectname".equalsIgnoreCase(method)){
				selectname(request,response);
			}
	}



	private void selectname(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name=request.getParameter("name");
		Student listu=stu.getStudentByname(name);
		boolean b=false;
			if(listu!=null){
				b=true;
			}else{
				b=false;
		}
			response.setContentType("application/json;charset=utf-8");
	    	String jsonStr = JSONArray.toJSONString(b);
	        //输出结果
	        response.getWriter().append(jsonStr);
	}

	private void selectgroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String name=request.getParameter("names");
			List<StudentVo> liststu=stu.getGroupStudent(name);
			request.setAttribute("lists", liststu);
			forward("interview/interview.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		get(request, response);
	}

	/**
	 * 登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws Exception{}
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void forward(HttpServletRequest request, HttpServletResponse response) throws Exception{
			forward("main.jsp");
	}
	
	/**
	 * 查询
	 * @param request
	 * @param response
	 * @throws SQLException 
	 */
	@SuppressWarnings("null")
	private void select(HttpServletRequest request, HttpServletResponse response) throws Exception {}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @throws SQLException 
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {}
	
	/**
	 *新增员工 
	 * @param request
	 * @param response
	 * @throws SQLException 
	 */
	private void insert(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name=request.getParameter("name");
		List<Student> listu=stu.getStudent();
		int a = listu.size();
		for (Student student : listu) {
			if(student.getStudentName().equalsIgnoreCase(name)){
				String errors=URLEncoder.encode("该员工已存在","utf-8");
				redirect("student?method=selectby&errors="+errors);
				break;
			}else{
				a= a-1;
				if(a==0){
					String sex=request.getParameter("sex");
					String address=request.getParameter("address");
					String phone=request.getParameter("phone");
					String qq=request.getParameter("qq");
					String recode=request.getParameter("recode");
					String parentname=request.getParameter("parentname");
					String parentphone=request.getParameter("parentphone");
					String source=request.getParameter("source");
					String interesting=request.getParameter("interesting");
					Student stus=new Student();
					stus.setStudentAddress(address);
					stus.setStudentInterestingId(Integer.parseInt(interesting));
					stus.setStudentName(name);
					stus.setStudentParent(parentname);
					stus.setStudentParentPhone(parentphone);
					stus.setStudentQq(qq);
					stus.setStudentRecode(recode);
					stus.setStudentSex(Integer.parseInt(sex));
					stus.setStudentSourceId(Integer.parseInt(source));
					stus.setStudentPhone(phone);
					stu.insertStudent(stus);
					redirect("student?method=selectby");
					break;
				}
			}
		}
		
	}

	/**
	 * 修改员工
	 * @param request
	 * @param response
	 * @throws SQLException 
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		Student student=stu.getStudent(id);
		List<Interesting> inlist=in.getAllInteresting();
		List<Source>  solist=new SourceDao().getAllSource();
		request.setAttribute("solist", solist);
		request.setAttribute("inlist", inlist);
		request.setAttribute("student",student);
		forward("student/student_update.jsp");
	}

	/**
	 * 修改员工
	 * @param request
	 * @param response
	 * @throws SQLException 
	 */
	private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		String address=request.getParameter("address");
		String phone=request.getParameter("phone");
		String qq=request.getParameter("qq");
		String recode=request.getParameter("recode");
		String parentname=request.getParameter("parentname");
		String parentphone=request.getParameter("parentphone");
		String source=request.getParameter("source");
		String interesting=request.getParameter("interesting");
		Student stus=new Student();
		stus.setStudentAddress(address);
		if(interesting!=null){
		stus.setStudentInterestingId(Integer.parseInt(interesting));
		}
		stus.setStudentName(name);
		stus.setStudentParent(parentname);
		stus.setStudentParentPhone(parentphone);
		stus.setStudentQq(qq);
		stus.setStudentRecode(recode);
		stus.setStudentSex(Integer.parseInt(sex));
		if(source!=null){
		stus.setStudentSourceId(Integer.parseInt(source));
		}
		stus.setStudentParentPhone(phone);
		stus.setStudentId(Integer.parseInt(id));
		stu.updateStudentById(stus);
		selectby(request,response);
	}
	/**
	 * 条件查询员工
	 * @param request
	 * @param response
	 * @throws SQLException 
	 */
	private void selectby(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String errors=request.getParameter("errors");
		if(errors!=null&&!errors.isEmpty()){
			request.setAttribute("errors", errors);
		}
		String name=request.getParameter("names");
		String isallot=request.getParameter("allot");
		String interesting=request.getParameter("interestings");
		String source=request.getParameter("sources");
		String staffname=request.getParameter("staffname");
		String page=request.getParameter("page");
		if(page==null||page.isEmpty()){
			page="1";
		}
		int pages=Integer.parseInt(page);
		//查询欧阳班级
		List<Clazz> clazzlist=clazz.getClazz();
		//总页数
		int totalPage=stu.getTotal(source, interesting, name, isallot,staffname);
		//条件查询学员信息
		List<StudentVo> list=	stu.getStudentBy(pages, source, interesting, name, isallot,staffname);
	
		//查询所有学员
		List<Map<String,Object>> lists=stu.getStudent(source, interesting, name, isallot,staffname);
		//共用数据
		int allcount=lists.size();
		//所有兴趣
		List<Interesting> inlist=in.getAllInteresting();
		//所有来源
		List<Source>  solist=new SourceDao().getAllSource();
		request.setAttribute("clazzlist", clazzlist);
		request.setAttribute("inlist", inlist);
		request.setAttribute("solist", solist);
		request.setAttribute("queryString", noPageQueryString());
		request.setAttribute("page", pages);
		request.setAttribute("allcount", allcount);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("list", list);
		//查询所有老师
		List<StaffPo> data = allotDao.getStaffList();
		if(data != null){
			request.setAttribute("StaffList", data);
		}
		forward("student/student.jsp");
	}
	
}
